﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Douban.Api.Authorization
{
    public abstract class OAuthAuthorization
    {
        private string _token;
        private string _tokenSecret;

        protected ConsumerBase Consumer { get; set; }

        public string Token
        {
            get { return _token; }
        }

        public string TokenSecret 
        { 
            get
            {
                return _tokenSecret;
            }
        }

        protected OAuthAuthorization(ConsumerBase consumer)
        {
            Consumer = consumer;
        }
        
        /// <summary>
        /// Begin OAuth authorize flow
        /// </summary>
        /// <returns>the user authorize uri</returns>
        public Uri BeginAuthorize()
        {
            return Consumer.ProcessUserAuthorization(
                GetServiceProviderDescriptor(), out _token, out _tokenSecret);
        }
        

        /// <summary>
        /// After user authorize, complete OAuth authorize flow
        /// </summary>
        public void CompleteAuthorize()
        {
            AuthorizedTokenResponse response = Consumer.ProcessUserAuthorization(
                new AuthorizedTokenRequest {Token = this.Token, TokenSecret = this.TokenSecret},
                GetServiceProviderDescriptor());

            _token = response.Token;
            _tokenSecret = response.TokenSecret;
        }
        
        public abstract ServiceProviderDescriptor GetServiceProviderDescriptor();
    }
}
